#!/bin/sh
str=${CONFIG_FILES}
password=${spring_cloud_nacos_password}
user_name=${spring_cloud_nacos_username}

get_access_token(){
  value=`echo $1 | sed 's/.*"accessToken":\([^,}]*\).*/\1/'`
  echo $value | sed 's/\"//g'
}
result=$(curl -X POST 'http://'${NACOS}'/nacos/v1/auth/users/login' --data-raw 'username='${user_name}'&password='${password})
echo 'result = '${result}
accessToken=$(get_access_token $result "accessToken")
echo "accessToken: $accessToken"

for i in `echo "$str" | sed 's/;/\n/g'`
do
   nacosstr=${i%%>*}
   dataid=${nacosstr##*@}
   nacosstr=${nacosstr%@*}
   group=${nacosstr##*@}
   tenant=${i%%@*}
   path=${i##*>}
   for j in `echo "$path" | sed 's/,/\n/g'`
   do
     echo 'curl-->dataid:'${dataid}' tenant:'${tenant}' path:'${j}' group:'${group};
     echo 'window.RPConfig = '`curl -L --request GET \
     --url http://${NACOS}/nacos/v1/cs/configs?tenant=${tenant}'&dataId='${dataid}'&accessToken'=${accessToken}'&group'=${group} \
     --header 'Cache-Control: no-cache' \
     --header 'Content-Type: application/json'` >${j}
   done
done;

# sed -i "48c proxy_pass  ${setting_proxy_pass}" /etc/nginx/nginx.conf
sed -i "57c proxy_pass  ${online_proxy_pass}" /etc/nginx/nginx.conf
sed -i "62c proxy_pass  ${modeler_proxy_pass}" /etc/nginx/nginx.conf
sed -i "67c proxy_pass  ${task_proxy_pass}" /etc/nginx/nginx.conf
sed -i "72c proxy_pass  ${admin_proxy_pass}" /etc/nginx/nginx.conf
sed -i "86c proxy_pass  ${gateway_proxy_pass}" /etc/nginx/nginx.conf
sed -i "99c proxy_pass  ${gateway_proxy_pass}" /etc/nginx/nginx.conf
exec nginx -g 'daemon off;'
